import 'package:fstudy/src/database/migration/v1.dart';
import 'package:fstudy/src/database/migration/v2.dart';
import 'package:sqflite/sqflite.dart';

abstract class AbsMigration {
  void migrate(Database db);

  void rollback(Database db) {}
}

class Migration {
  static Map<int, AbsMigration> versions = {1: V1(), 2: V2()};

  // 迁移，创建、升级
  static void migrate(Database db, int newVersion, {int oldVersion = 0}) {
    for (int i = oldVersion; i <= newVersion; i++) {
      versions[i]?.migrate(db);
    }
  }

  // 回滚
  static void rollback(Database db, int oldVersion, {int newVersion = 0}) {
    for (int i = oldVersion; i >= newVersion; i--) {
      versions[i]?.rollback(db);
    }
  }
}
